import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { WorkshopModel } from '#/api/master/models';

/**
 * 获取编辑表单的字段配置。
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      dependencies: {
        triggerFields: ['wspCode'],
        disabled(values) {
          return !!values.id;
        },
      },
      fieldName: 'wspCode',
      label: '车间编码',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'wspName',
      label: '车间名称',
      rules: 'required',
    },
    {
      component: 'Switch',
      componentProps: {
        checkedValue: 1,
        unCheckedValue: 0,
        checkedChildren: '启用',
        unCheckedChildren: '禁用',
        class: 'w-auto',
      },
      fieldName: 'status',
      label: '状态',
      defaultValue: 1,
      rules: 'required',
    },
    {
      component: 'Textarea',
      componentProps: {
        showCount: true,
        maxlength: 128,
      },
      fieldName: 'remark',
      label: '备注',
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<WorkshopModel>,
): VxeTableGridOptions<WorkshopModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'wspCode', title: '车间代码', width: 140 },
    { field: 'wspName', title: '车间名称', width: 220 },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: { name: 'CellTag', attrs: { useStatus: true } },
    },
    { field: 'remark', title: '备注' },
    { field: 'createdTime', title: '创建时间', width: 160 },
    {
      cellRender: {
        attrs: {
          nameField: 'wspName',
          nameTitle: '车间',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          'edit', // 默认的编辑按钮
          'delete', // 默认的删除按钮
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 120,
    },
  ];
}
